package second.string;

public class Solution02 {
    public String reverseStr(String s, int k) {
        char[] chars = s.toCharArray();
        for (int i = 0; i < s.length(); i += 2 * k) {
            if (i + k < s.length()) {
                reverse(chars, i, i + k - 1);
            } else {
                reverse(chars, i, s.length() - 1);
            }
        }
        return new String(chars);
    }

    public void reverse(char[] chars, int i, int j) {
        int mid = (i + j) >> 1;
        for (int k = i; k <= mid; k++) {
            char tmp = chars[k];
            chars[k] = chars[j - (k - i)];
            chars[j - (k - i)] = tmp;
        }
    }
}
